package com.taomu.app.zongchouhuli.moudle.mine.contract.impl;

import com.alibaba.fastjson.JSONObject;
import com.taomu.app.zongchouhuli.entity.BaseChangeResponse;
import com.taomu.app.zongchouhuli.entity.BaseResponse;
import com.taomu.app.zongchouhuli.http.api.HttpObserver;
import com.taomu.app.zongchouhuli.http.api.Request;
import com.taomu.app.zongchouhuli.http.res.ResChoiceRechargeEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyBuyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResMyProductEntity;
import com.taomu.app.zongchouhuli.http.res.ResProductDetailEntity;
import com.taomu.app.zongchouhuli.http.res.ResUserInfoEntity;
import com.taomu.app.zongchouhuli.moudle.mine.contract.UserRebateListContract;
import com.taomu.app.zongchouhuli.moudle.mine.entity.RebateListEntity;
import com.taomu.app.zongchouhuli.utils.ParseUtil;
import com.taomu.app.zongchouhuli.utils.StorageHelper;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.CompositeDisposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;

public class UserRebateListPresenter implements UserRebateListContract.Presenter {

    private UserRebateListContract.View mContractView;

    private CompositeDisposable disposable;

    public UserRebateListPresenter(UserRebateListContract.View mConatractView){
        this.mContractView = mConatractView;
        this.disposable = new CompositeDisposable();
    }

    @Override
    public void getRebateList(int ship, int take) {
        Request.getIntence().getMyProductList(String.valueOf(ship), String.valueOf(take))
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<ResMyBuyProductEntity>(mContractView,disposable,true) {
                    @Override
                    public void onNext(ResMyBuyProductEntity baseResponse) {
                        if(mContractView != null){
                            mContractView.setRebateData(baseResponse);
                        }
                    }
                });
    }

    @Override
    public void getRebateList() {
        setRebateList();
    }

    @Override
    public void detch() {

    }

    @Override
    public void tixian(String moneystr) {
        Request.getIntence().getTranferProductAccount(moneystr, "1")
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<String>(mContractView,disposable,true) {
                    @Override
                    public void onNext(String baseResponse) {
                        if(mContractView != null){
                            mContractView.setMsg(baseResponse);
                        }
                    }
                });
    }

    @Override
    public void getUserInfo() {
        Request.getIntence().getUserInfo(StorageHelper.getToken())
                .flatMap(new Function<String, ObservableSource<BaseChangeResponse<ResUserInfoEntity>>>() {
                    @Override
                    public ObservableSource<BaseChangeResponse<ResUserInfoEntity>> apply(String json) throws Exception {
                        BaseResponse<com.alibaba.fastjson.JSONObject> baseResponse = ParseUtil.parseObject(json,BaseResponse.class);
                        BaseChangeResponse<ResUserInfoEntity> data = new BaseChangeResponse<>();
                        data.setData(ParseUtil.parseObject(baseResponse.getData().toString(),ResUserInfoEntity.class));
                        data.setCount(baseResponse.getCount());
                        return Observable.just(data);
                    }
                })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new HttpObserver<BaseChangeResponse<ResUserInfoEntity>>(mContractView,disposable,true) {
                    @Override
                    public void onNext(BaseChangeResponse<ResUserInfoEntity> baseResponse) {
                        if(mContractView != null){
                            StorageHelper.saveToken(baseResponse.getData().getToken());
                            StorageHelper.saveAccountInfo(ParseUtil.toJson(baseResponse.getData()));
                            mContractView.setTotalMoney(baseResponse.getData().getProduct_account());
                        }
                    }
                });
    }

    public void setRebateList(){
        List<RebateListEntity> mData = new ArrayList<>();
        for(int i=0;i<10;i++){
            RebateListEntity entity = new RebateListEntity();
            entity.setName("产品-"+String.valueOf(i));
            entity.setPrice("666");
            entity.setBalance("日结");
            entity.setIncome("2%");
            entity.setRebate("400");
            entity.setTime("2018/08/31");
            mData.add(entity);
        }
        if(mContractView != null){
            mContractView.setRebateList(mData);
        }
    }

    @Override
    public void onStart() {

    }

    @Override
    public void onDetch() {
        mContractView = null;
        if(disposable != null){
            disposable.dispose();
            disposable = null;
        }
    }
}
